package controller

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Number"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/repository"
	"go-fox-edge-kernel-manager/utils/PageUtils"
	"strconv"
)

type repoLocalCompListController struct {
}

func (e *repoLocalCompListController) init() {
	httpserver.Server.POST("/repository/local/comp-list/entities", e.selectEntityList)
	httpserver.Server.POST("/repository/local/comp-list/page", e.selectPageList)
	httpserver.Server.GET("/repository/local/comp-list/entity", e.queryEntity)
	httpserver.Server.POST("/repository/local/comp-list/entity", e.insertEntity)
	httpserver.Server.PUT("/repository/local/comp-list/entity", e.updateEntity)
	httpserver.Server.DELETE("/repository/local/comp-list/entity", e.deleteEntity)

	httpserver.Server.POST("/repository/local/comp-list/sync", e.syncCloud)
	httpserver.Server.POST("/repository/local/comp-list/upload", e.uploadCloud)
}

func (e *repoLocalCompListController) selectEntityList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, false)
}

func (e *repoLocalCompListController) selectPageList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.selectList(context, body, true)
}

func (e *repoLocalCompListController) selectList(context httpserver.Context, body map[string]interface{}, isPage bool) (interface{}, error) {
	// 提取业务参数
	compType := Map.GetString(body, "compType", "")
	pageNum := Map.GetInt(body, "pageNum", 1)
	pageSize := Map.GetInt(body, "pageSize", 10)

	// 简单校验参数
	if Map.HasNull(body, "compType", "pageNum", "pageSize") {
		return nil, errors.New("参数不能为空: compType, pageNum, pageSize")
	}

	entityList, err := repository.LocalCompService.GetCompEntityList(body)
	if err != nil {
		return nil, err
	}

	mapList := make([]map[string]interface{}, 0)

	// 服务场景：进行登记排序
	if compType == "app-service" {
		entityList = repository.LocalApplication.Sort(entityList)
		mapList = commEntity.BuildMapList(entityList)
	} else if compType == "jar-decoder" {
		mapList = repository.LocalJarFileInfo.ExtendCompJarInfo(entityList)
	} else if compType == "jsp-decoder" {
		mapList = repository.LocalScript.ExtendCompOperateInfo(entityList)
	} else {
		commEntity.SortEntityList(entityList)
		mapList = commEntity.BuildMapList(entityList)
	}

	// 获得分页数据
	if isPage {
		return PageUtils.GetMapPageList(mapList, pageNum, pageSize), nil
	} else {
		return commEntity.BuildMapList(entityList), nil
	}
}

func (e *repoLocalCompListController) queryEntity(context httpserver.Context) (interface{}, error) {
	str := context.Query("id")
	if str == "" {
		return nil, errors.New("输入的id为null")
	}

	id, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		return nil, err
	}

	exist := repository.LocalCompService.GetRepoCompEntity(Number.MakeInt64(id))
	if exist == nil {
		return nil, errors.New("实体不存在")
	}

	return commEntity.BuildMap(exist), nil
}

func (e *repoLocalCompListController) insertEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	delete(body, "id")
	return e.insertOrUpdate(context, body)
}

func (e *repoLocalCompListController) updateEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.insertOrUpdate(context, body)
}

func (e *repoLocalCompListController) insertOrUpdate(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	// 构造作为参数的实体
	entity, err := repository.LocalCompBuilder.BuildCompEntity(body)
	if err != nil {
		return nil, err
	}

	// 新增/修改实体：参数不包含id为新增，包含为修改
	if !Map.Has(body, "id") {
		exist, err := commEntityManager.GetEntityByServiceKey(entity.Type(), entity.MakeServiceKey())
		if err != nil {
			return nil, err
		}
		if exist != nil {
			return nil, errors.New("实体已存在")
		}

		err = commEntityManager.InsertEntity(entity)
		if err != nil {
			return nil, err
		}

		return nil, nil
	} else {
		id := Map.GetInt64(body, "id", 0)

		exist := commEntityManager.GetEntityById(entity.Type(), id)
		if exist == nil {
			return nil, errors.New("实体不存在")
		}

		// 修改数据
		entity.BaseEntity.Id = id
		entity.GetBaseEntity().CreateTime = exist.GetBaseEntity().CreateTime
		entity.GetBaseEntity().UpdateTime = exist.GetBaseEntity().UpdateTime
		return nil, commEntityManager.UpdateEntity(entity)
	}
}

func (e *repoLocalCompListController) deleteEntity(context httpserver.Context) (interface{}, error) {
	str := context.Query("id")
	if str == "" {
		return nil, errors.New("输入的id为null")
	}

	id, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		return nil, err
	}

	return nil, repository.LocalCompService.DeleteCompEntity(Number.MakeInt64(id))
}

func (e *repoLocalCompListController) syncCloud(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	if !Map.Has(body, "id") {
		return nil, errors.New("参数不能为空: id")
	}

	id := Map.GetInt64(body, "id", 0)

	return repository.LocalCompSync.SyncEntity(id)
}

func (e *repoLocalCompListController) uploadCloud(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	if !Map.Has(body, "id") {
		return nil, errors.New("参数不能为空: id")
	}

	id := Map.GetInt64(body, "id", 0)
	commitKey := Map.GetString(body, "commitKey", "")
	description := Map.GetString(body, "description", "")

	// 简单校验参数
	if Method.HasEmpty(commitKey, description) {
		return nil, errors.New("参数不能为空: commitKey, description")
	}

	return repository.LocalCompUpload.UploadEntity(id, commitKey, description)
}
